package com.shouhuobao.take.verify;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.app.WebViewActivity;
import com.shouhuobao.station.R;
import com.shouhuobao.take.utils.AppTab;

import droid.frame.view.ViewPagerExt;

/**
 * 选择驿站级别activity
 */
public class RegVerifyRankActivity extends BaseActivity2 implements View.OnClickListener {

    private ViewPagerExt mViewPager = null;
    private int type = 2;// 1 初级  2 标准 3 专业
    private String quoteCost1 = "36.5";//
    private String quoteCost2 = "365";//
    private String quoteCost3 = "1980";//

    private BaseFragment mFragment1, mFragment2, mFragment3;
    private CheckBox mSureCB;
    private TextView mPayMoneyTV;

    private String payMoneyHtmlStr = "<font color=#FFFFFF>需付款：</font><font color=#FDD120>%1$s</font><font color=#FFFFFF>元</font>";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_reg_verify_rank);
        super.findViewById();
        getAppTitle().setCommonTitle("选择驿站级别");
        TextView[] mTags = new TextView[2];
        //        mTags[0] = (TextView) findViewById(R.id.verify_primer_rank_tv);
        mTags[0] = (TextView) findViewById(R.id.verify_standard_rank_tv);
        mTags[1] = (TextView) findViewById(R.id.verify_major_rank_tv);
        mViewPager = (ViewPagerExt) findViewById(R.id.viewpager);
        mPayMoneyTV = (TextView) findViewById(R.id.verify_pay_money_tv);
        mSureCB = (CheckBox) findViewById(R.id.verify_cb);
        mSureCB.setChecked(true);
        findViewById(R.id.read_lower_tv).setOnClickListener(this);
        //立即开通
        findViewById(R.id.station_open_tv).setOnClickListener(this);
        ImageView mImgLine = (ImageView) findViewById(R.id.reg_line_img);

        initFragment();

        mViewPager.setFragmentItems(getSupportFragmentManager(), mFragment2, mFragment3);
        mViewPager.setSupportScroll(true);
        AppTab appTab = new AppTab(getContext(), 0);
        appTab.initPosition(mViewPager, mImgLine, mTags);
        mViewPager.setOffscreenPageLimit(2);

        mPayMoneyTV.setText(Html.fromHtml(String.format(payMoneyHtmlStr, quoteCost2)));

        mViewPager.addOnPageChangeListener(mViewPager.new ViewPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                type = position + 2;
                //                if (position == 0) {
                //                    mPayMoneyTV.setText(Html.fromHtml(String.format(payMoneyHtmlStr, quoteCost1)));
                //                } else
                if (position == 0) {
                    mPayMoneyTV.setText(Html.fromHtml(String.format(payMoneyHtmlStr, quoteCost2)));
                } else {
                    mPayMoneyTV.setText(Html.fromHtml(String.format(payMoneyHtmlStr, quoteCost3)));
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            //阅读协议条款
            case R.id.read_lower_tv:
                intent = new Intent();
                intent.setClass(getContext(), WebViewActivity.class);
                intent.putExtra("title", "驿站开通条款");
                startActivity(intent);
                break;
            //立即开通
            case R.id.station_open_tv:
                if (mSureCB.isChecked()) {
                    intent = new Intent(getContext(), VerifyOrderPayActivity.class);
                    intent.putExtra("type", type);
                    if (type == 1) {
                        intent.putExtra("quoteCost", quoteCost1);
                    } else if (type == 2) {
                        intent.putExtra("quoteCost", quoteCost2);
                    } else {
                        intent.putExtra("quoteCost", quoteCost3);
                    }
                    startActivityForResult(intent, 101);
                } else {
                    showToast("请阅读加入《申请协议》");
                }
                break;
        }

    }

    private void initFragment() {
//        mFragment1 = new RegRank1Fragment();
//        Bundle bundle1 = new Bundle();
//        bundle1.putInt("type", 0);
//        mFragment1.setArguments(bundle1);
        //
        Bundle bundle2 = new Bundle();
        bundle2.putInt("type", 1);
        mFragment2 = new RegRank2Fragment();
        mFragment2.setArguments(bundle2);
        //
        Bundle bundle3 = new Bundle();
        bundle3.putInt("type", 2);
        mFragment3 = new RegRank3Fragment();
        mFragment3.setArguments(bundle3);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (101 == requestCode) {
            finish();
        }
    }
}
